<?php

if (!defined('IN_UkCMS'))
exit('No direct script access allowed');

class User_mdl extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function get_full_user_by_username($username = '') {
		return $this->db->select('uk_admins.uid , uk_admins.password , uk_admins.role , uk_roles.name')
						->from('uk_admins')
						->join('uk_roles', 'uk_roles.id = uk_admins.role')
						->where('uk_admins.username', $username)
						->get()
						->row();
	}

	function get_user_by_uid($uid = 0) {
		return $this->db->where('uid', $uid)->get('uk_admins')->row();
	}

	function get_user_by_name($name) {
		return $this->db->where('username', $name)->get('uk_admins')->row();
	}

	function update_user_password() {
		$data['password'] = md5($this->input->post('new_pass'));
		$this->db->where('uid', $this->session->userdata('uid'))->update('uk_admins', $data);
	}

	function get_roles() {
		$roles = array();
		foreach ($this->db->select('id,name')->where('id <>', 1)->get('uk_roles')->result_array() as $v) {
			$roles[$v['id']] = $v['name'];
		}
		return $roles;
	}

	function get_users($role_id = 0) {
		$this->db->where('uk_admins.uid <>', 1);
		if ($role_id) {
			$this->db->where('uk_admins.role', $role_id);
		}
		return $this->db->from('uk_admins')
						->join('uk_roles', 'uk_roles.id = uk_admins.role')
						->get()
						->result();
	}

	function add_user($data) {
		$this->load->helper('date');
		$data['reg_date'] = now();
		$this->db->insert('uk_admins', $data);
	}

	function edit_user($uid, $data) {
		$this->db->where('uid', $uid)->update('uk_admins', $data);
	}

	function del_user($uid) {
		$this->db->where('uid', $uid)->delete('uk_admins');
	}

}